﻿using CentaLine.Infrastructure;
using CentaLine.Model;
using CentaLine.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CentaLine.Site.Controllers
{
    public class HomeController : BaseController
    {
        DirectorManager dm;
        VitualPhoneManager vp;
        CounselorManager cm;
        public HomeController()
        {
            dm = new DirectorManager();
            vp = new VitualPhoneManager();
            cm = new CounselorManager();
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult GetAppList(string appMonth, int page = 1, int pageSize = 10, string counselorId = null, string directorId = null, string status=null)
        {
            //TODO 获取申报列表时 立即同步最新数据
            DateTime dt = DateTime.Now;
            if (!string.IsNullOrEmpty(appMonth))
            {
                DateTime.TryParse(appMonth, out dt);
            }
            int skip = (page - 1) * pageSize;
            var res = dm.GetAllCounselorApplicationList(dt,directorId,counselorId,status ).Skip(skip).Take(pageSize);
            return Json(res,JsonRequestBehavior.AllowGet);
        }
        public ActionResult GetAppListCount(string appMonth, string counselorId = null, string directorId = null, string status = null)
        {
            //TODO 获取申报列表时 立即同步最新数据
            DateTime dt = DateTime.Now;
            if (!string.IsNullOrEmpty(appMonth))
            {
                DateTime.TryParse(appMonth, out dt);
            }
            vp.AsyncCounselorApplicationList(dt);
            var res = dm.GetAllCounselorApplicationList(dt,directorId,counselorId,status).Count;
            return Json(res, JsonRequestBehavior.AllowGet);
        }

        public string Welocome(string name,int id=1)
        {
            return HttpUtility.HtmlEncode("Hello"+name+"id"+id);
        }

        public ActionResult About()
        {
            ViewBag.Message = "你的应用程序说明页。";
            GetHandlerStatus();
            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "你的联系方式页。";

            return View();
        }

        public ActionResult GetHandlerStatus()
        {
           var dtStatus = new Dictionary<string, string>();
           foreach(var code  in Enum.GetValues(typeof(HandlerStatus)))
           {
             string key  = code.ToString();
             string value = Enum.GetName(typeof(HandlerStatus), code);
             dtStatus.Add(key,value);
           }
           return Json(dtStatus,JsonRequestBehavior.AllowGet);
        }

        public ActionResult GetWebList()
        {
            var res = vp.GetPhoneKindList();
            return Json(res, JsonRequestBehavior.AllowGet);
        }

        public ActionResult SingleHandler(string counselorId,int webId,string webName,string mobile)
        {
            var res = new CommResult();
            var counselor =  cm.GetCounselor(counselorId);
            if (counselor == null)
            {
                res.Result = false;
                res.Exception = "无法在组织库中获取到该业务员的信息";
                return Json(res);
            }
            if (!counselor.IsWorking)
            {
                res.Result = false;
                res.Exception = "该员工已经离职";
                return Json(res);
            }
            // CounselorApplication c = new CounselorApplication(appMonth, "测试业务员", counselorId, "测试组别", "测试总监", "testid", pk, mobile);
            var c = new CounselorApplication(CommonHelper.DateForamt(DateTime.Now), counselorId, counselor.Director.Id, new PhoneKind(webId, webName), mobile);
            c.DirectorName = counselor.Director.Name;
            c.CounselorName = counselor.Name;
            string id = vp.AddCounselorApplication(c);
            res = vp.SingleAssign(id);
            return Json(res);
        }

        public ActionResult SingleHandlerWithId(string id)
        {
            var res = vp.SingleAssign(id);
            return Json(res);
        }

        //释放某个虚拟号码
        public ActionResult ReleaseNum(string vitualPhoneNumList)
        {
            bool res = true;
            var vitulPhoneArr = vitualPhoneNumList.Split(',');
            foreach (var item in vitulPhoneArr)
            {
                if (!vp.ReleaseSingleNum(item,"网站手动解绑虚拟号码"+item))
                {
                    res = false;
                }
            }
            return Json(res,JsonRequestBehavior.AllowGet);
        }

        //返回某个真实号码对应的虚拟号码列表
        public ActionResult GetPhoneListFromTdialnumber(string actualNo)
        {
            var res = dm.GetAllPhoneListFromTdialnumber(actualNo);
            return Json(res, JsonRequestBehavior.AllowGet);
        }
    }
}
